#include "stdafx.h"
#include "File.h"

File::File(String* name,uint access,uint creation){
	create(name->Ptstr(),access,creation);
};
File::File(TCHAR* name,uint access,uint creation){
	create(name,access,creation);
};
File::~File(){
	CloseHandle(handle);
}

void File::create(PTSTR filename,uint access,uint creation){
	handle=CreateFile(filename,GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,
		FILE_ATTRIBUTE_NORMAL,NULL);
	if(handle==INVALID_HANDLE_VALUE)
		throw(0);
}

Bin* File::get(void){
	LARGE_INTEGER n;
	GetFileSizeEx(handle,&n);
	if(n.HighPart)
		throw(0);
	Bin* res=new Bin(n.LowPart);
	memset(res->Ptr(),0,res->getLength());
	ReadFile(handle,res->Ptr(),res->getLength(),&n.LowPart,NULL);
	return res;
}
void File::write(Bin* bin){
	if(handle!=INVALID_HANDLE_VALUE){
		uint n;
		WriteFile(handle,bin->Ptr(),bin->getLength(),&n,0);
	}
}